<?php

/**
 * 客栈列表
 */
class roomAction extends globalAction {
    private $dao, $inn;
    
    public function _initialize() {
		parent::_initialize();
        parent::_loginRequire();
        /***********************************************************************
         * 客栈管理权限验证
         **********************************************************************/
        $innId = intval($_REQUEST['innId']);
        if(empty($innId)) parent::_message('error','未指定客栈');
        $innDao = D('Inn');
        $this->inn = $innDao
            ->field($innDao->_map_flip)
            ->where(array(
                $innDao->_map['id'] => $innId,
                $innDao->_map['menuId'] => 585,
                $innDao->_map['uid'] => __USERID__
            ))
            ->find();
        if(empty($this->inn)) parent::_message('error','您无权管理此客栈');
        $this->assign('inn', $this->inn);
        
        $this->dao = D('InnRoom');
	}
    /**
     * request $_GET['innId'] OR ($_GET['innId'] AND $_GET['roomid'])
     */
    public function index() {
        
        /***********************************************************************
         * 查找房型列表
         **********************************************************************/
        $roomList = $this->dao
            ->field($this->dao->_map_flip)
            ->where(array(
                $this->dao->_map['menuId'] => 601,
                $this->dao->_map['innId'] => $this->inn['id']
            ))
            ->order(array(
                $this->dao->_map['display_order'] => 'asc'
            ))
            ->select();
        $this->assign('roomList', $roomList);
        
        /***********************************************************************
         * 确定当前查看房型
         **********************************************************************/
        $roomId = intval($_GET['roomid']);
        if(!empty($roomId)) {
            foreach($roomList as $roomRow) {
                if($roomRow['id']==$roomId) {
                    $room = $roomRow;
                    $this->assign('room', $room);
                    break;
                }
            }
        }
        /***********************************************************************
         * 房型图片列表
         **********************************************************************/
        if(!empty($room)) {
            $InnRoomImagesDao = D('InnRoomImages');
            $InnRoomImagesList = $InnRoomImagesDao
                ->field($InnRoomImagesDao->_map_flip)
                ->where(array(
                    $InnRoomImagesDao->_map['menuId'] => 616,
                    $InnRoomImagesDao->_map['roomId'] => $room['id']
                ))
                ->order($InnRoomImagesDao->_map['display_order'])
                ->select();
            $this->assign('InnRoomImagesList', $InnRoomImagesList);
        }
        /***********************************************************************
         * 房型类别列表
         **********************************************************************/
        $roomTypeDao = D('InnRoomType');
        $roomTypeList = $roomTypeDao
            ->field($roomTypeDao->_map_flip)
            ->where(array(
                $roomTypeDao->_map['menuId'] => 617
            ))
            ->order($roomTypeDao->_map['display_order'])
            ->select();
        $this->assign('roomTypeList', makearray($roomTypeList, 'id', 'name'));
        
        /***********************************************************************
         * 预定类别列表
         **********************************************************************/
        $roomOrderTypeDao = D('InnRoomOrderType');
        $roomOrderTypeList = $roomOrderTypeDao
            ->field($roomOrderTypeDao->_map_flip)
            ->where(array(
                $roomOrderTypeDao->_map['menuId'] => 619
            ))
            ->order($roomOrderTypeDao->_map['display_order'])
            ->select();
        $this->assign('roomOrderTypeList', makearray($roomOrderTypeList, 'id', 'name'));
        
        $this->display();
    }
    
    /**
     * request $_POST['innId'] AND ($_POST['add'] OR ($_POST['modify'] AND $_POST['id']))
     */
    public function do_room() {
        if($this->dao->create()) {
            if(!empty($_POST['add'])) {
                $result = $this->dao->add();
                if(false !== $result) {
                    parent::_message('success', '添加成功！', C('TMPL_PARSE_STRING.__KZURL__').'/room/index-innId-'.$this->inn['id'].'-roomid-'.$result);
                } else {
                    parent::_message('error', '添加失败！请联系管理员！', C('TMPL_PARSE_STRING.__KZURL__').'/room/index-innId-'.$this->inn['id']);
                }
            } elseif(!empty($_POST['modify']) && !empty($_POST['id'])) {
                $result = $this->dao->save();
                if(false !== $result) {
                    parent::_message('success', '修改成功！', C('TMPL_PARSE_STRING.__KZURL__').'/room/index-innId-'.$this->inn['id'].'-roomid-'.$_POST['id']);
                } else {
                    parent::_message('error', '修改失败！请联系管理员！', C('TMPL_PARSE_STRING.__KZURL__').'/room/index-innId-'.$this->inn['id'].'-roomid-'.$_POST['id']);
                }
            }
        } else {
            parent::_message('error', implode('<br />', $this->dao->getError()), $_SERVER['HTTP_REFERER']);
        }
    }
    
    /**
     * request $_GET['innId'] AND $_GET['roomid']
     */
    public function do_delete() {
        
        //查找房型信息
        $roomId = intval($_GET['roomid']);
        $room = $this->dao
            ->field($this->dao->_map_flip)
            ->where(array(
                $this->dao->_map['id'] => $roomId,
                $this->dao->_map['menuId'] => 601,
                $this->dao->_map['innId'] => $this->inn['id']
            ))
            ->find();
        if(empty($room)) parent::_message('error', '未找到房型信息');

        //删除房型图片列表
        $InnRoomImagesDao = D('InnRoomImages');
        $InnRoomImagesList = $InnRoomImagesDao
            ->field($InnRoomImagesDao->_map_flip)
            ->where(array(
                $InnRoomImagesDao->_map['menuId'] => 616,
                $InnRoomImagesDao->_map['roomId'] => $room['id']
            ))
            ->order($InnRoomImagesDao->_map['display_order'])
            ->select();
        $deletedId = array();
        foreach($InnRoomImagesList as $pic) {
            @unlink('.'.$pic['path']);
            $deletedId[] = $pic['id'];
        }
        $InnRoomImagesDao->where(array(
            $InnRoomImagesDao->_map['id'] => array('in',$deletedId)
        ))->delete();
        
        //TODO 删除房型价格
        
        //删除房型数据
        $this->dao
            ->where(array(
                $this->dao->_map['id'] => $room['id'],
                $this->dao->_map['menuId'] => 601,
                $this->dao->_map['innId'] => $this->inn['id']
            ))
            ->delete();
        redirect(C('TMPL_PARSE_STRING.__KZURL__').'/room/index-innId-'.$this->inn['id']);
    }
    
    /**
     * Ajax上传客栈图集
     */
    public function upimages() {
        //查找房型信息
        $roomId = intval($_POST['id']);
        if(empty($roomId)) die('未指定房型信息！');
        $room = $this->dao
            ->field($this->dao->_map_flip)
            ->where(array(
                $this->dao->_map['menuId'] => 601,
                $this->dao->_map['id'] => $roomId,
                $this->dao->_map['innId'] => $this->inn['id']
                
            ))
            ->find();
        if(empty($room)) die('未找到房型信息');

        //准备接收上传文件
        import('ORG.Net.UploadFile');
        $this->upload = new UploadFile();
        $this->upload->maxSize = 1024*1024;      //1M
        $this->upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');
        $this->upload->savePath =  UPLOAD_PATH;  //文件保存目录
        $this->upload->autoSub = true;           //自动创建子目录
        $this->upload->subType = 'date';         //按日期创建子目录
        $this->upload->dateFormat = 'Ym/d';      //日期子目录规则（前后不加'/'）
        $this->upload->saveRule = 'uniqid';      //文件名生成规则
        
        //处理文件上传
        if(!$this->upload->upload()) {
            die('错误提示：文件上传失败！');
        } else {
            $InnRoomImagesDao = D('InnRoomImages');
            $info =  $this->upload->getUploadFileInfo();
            foreach($info as $upinfo) {
                $picpath = strtr(UPLOAD_PATH, array('.'=>'')) . $upinfo['savename'];
                //保存新图片
                $data = array(
                    $InnRoomImagesDao->_map['path'] => $picpath,
                    $InnRoomImagesDao->_map['roomId'] => $roomId,
                );
                if($InnRoomImagesDao->create($data))
                    if($picid = $InnRoomImagesDao->add())
                        die(json_encode(array(
                            "picid" => $picid,
                            "picpath" => $picpath
                        )));
            }
        }
        die('错误提示：文件保存出错！');
    }
    
    /**
     * Ajax删除客栈图集
     */
    public function delimages() {
        $imgid = intval($_POST['imgid']);
        $roomId = intval($_POST['id']);
        if(empty($imgid)) die('没有指定要删除的图片！');
        if(empty($roomId)) die('没有指定房型ID！');
        
        
        //查找房型信息
        if(empty($roomId)) die('未指定房型信息！');
        $room = $this->dao
            ->field($this->dao->_map_flip)
            ->where(array(
                $this->dao->_map['menuId'] => 601,
                $this->dao->_map['id'] => $roomId,
                $this->dao->_map['innId'] => $this->inn['id']
            ))
            ->find();
        if(empty($room)) die('未找到房型信息');
        
        $InnRoomImagesDao = D('InnRoomImages');
        $InnRoomImagesRow = $InnRoomImagesDao
            ->field($InnRoomImagesDao->_map_flip)
            ->where(array(
                $InnRoomImagesDao->_map['menuId'] => 616,
                $InnRoomImagesDao->_map['roomId'] => $room['id'],
                $InnRoomImagesDao->_map['id'] => $imgid,
            ))
            ->find();
        if(empty($InnRoomImagesRow)) die('没有找到要删除的图片！');

        @unlink('.'.$InnRoomImagesRow['path']);
        $InnRoomImagesDao->where(array($InnRoomImagesDao->_map['id'] => $imgid))->delete();
        die('1');
    }
}